var CustomerAppointment = {
    id: "CustomerAppointmentTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function _formatter(value, row, index) {
    if (row.accountArrears > 0) {
        return '<span style="color: red" title="' + value + '">' + value + '</span>'
    } else {
        return '<span title="' + value + '">' + value + '</span>'
    }
}

function _formatterTime(value, row, index) {
    if (row.accountArrears > 0) {
        return "<span style='color: red'>" + value.substring(0, 19) + "</span>"
    } else {
        return value.substring(0, 19);
    }
}

CustomerAppointment.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.accountArrears > 0) {
                    return "<a href='javascript:;' style='color: red;display: flex;align-items: center;' onclick = 'CustomerAppointment.openBilee(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                } else {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CustomerAppointment.openBilee(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            }
        },
        {title: '电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '电话2', field: 'customerPhone2', align: 'center', valign: 'middle', sortable: true, width: 135,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '使用人', field: 'phoneSharShip', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '性别', field: 'sex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '科室', field: 'treatmentUnit', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true,  width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '已交金额', field: 'actualAccountBalance', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (row.accountArrears > 0) {
                    return "<a href='javascript:;' style='color: red'>" + (value +row.moneyBalance )+ "</a>"
                } else {
                    return (value +row.moneyBalance );
                }
            }
        },
        {title: '欠费', field: 'accountArrears', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '开始时间', field: 'startTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '结束时间', field: 'endTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '初诊时间', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '黄页编号', field: 'yellowNumber', align: 'center', valign: 'middle', sortable: true, width: 140,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}
            }
        },
        {title: '网电咨询师', field: 'scarchiving', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '一级媒介', field: 'oneMedium', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '二级媒介', field: 'twoMedium', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '三级媒介', field: 'thrMedium', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        }
    ];
    return columns;
};
//表单查询提交参数
CustomerAppointment.formParams = function () {
    var queryData = {};
    queryData['sreach'] = $("#sreach").val();
    queryData['customerAppointmentName'] = $("#customerAppointmentName").val();
    queryData['customerAppointmentPhone'] = $("#customerAppointmentPhone").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['reservations'] = $("#reservations").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['doctor'] = $("#doctor").val();
    queryData['customerStatus'] = $("#customerStatus").val();
    queryData['treatmentUnit'] = $("#treatmentUnit").val();
    queryData['oneChannel'] = multipleSelection($('#oneChannel').val());
    queryData['twoChannel'] = multipleSelection($('#twoChannel').val());
    queryData['thrChannel'] = multipleSelection($('#thrChannel').val());
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    return queryData;
};
//把对象拼接成URL的键值对参数 param1=value1&param2=value2
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};
//重置
CustomerAppointment.resetSearch = function () {
    $("#sreach").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#fieldTc").val("");
    $("#doctor").val("");
    $("#customerStatus").val("");
    $('#oneChannel').selectpicker('val', ['noneSelectedText'])//回到初始状态
    $('#twoChannel').selectpicker('val', ['noneSelectedText'])//回到初始状态
    $('#thrChannel').selectpicker('val', ['noneSelectedText'])//回到初始状态
    $(".timeBox > div button").removeClass('actionClick')
    CustomerAppointment.search();
};
//跳转到发送短信
CustomerAppointment.shortMessage = function () {
    if (this.check()) {
        var customerAppointmentId = CustomerAppointment.seItem.id;
        var index = layer.open({
            type: 2,
            title: '发送消息',
            area: ['60%', '80%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/msgSend/to_msgSend/' + customerAppointmentId
        });
        this.layerIndex = index;
    }
};
//短信发送
CustomerAppointment.msgSend = function () {
    var customerAppointmentId = $("#customerAppointmentId").val();
    var customerAppointmentName = $("#customerAppointmentName").val();
    var customerAppointmentPhone = $("#customerAppointmentPhone").val();
    var messageContent = $("#message_content").val();
    var outpatientName = $("#outpatientName").val();
    var address = $("#address").val();
    var phone = $("#phone").val();
    var time = $("#startTimeValue1").val();
    var doctor = $("#doctorValue1").val();
    var templateCode = $("#templateCodeValue").val();
    if (confirm("发送信息给【" + customerAppointmentName + "】?")) {
        $.ajax({
            url: Feng.ctxPath + "/msgSend/msgSend",
            type: "post",
            data: {
                customerAppointmentId: customerAppointmentId,
                messageContent: messageContent,
                templateCode: templateCode,
                customerAppointmentPhone: customerAppointmentPhone,
                outpatientName: outpatientName,
                address: address,
                phone: phone,
                time: time,
                doctor: doctor,
                customerAppointmentName:customerAppointmentName
            },
            success: function (data) {
                Feng.success('操作成功!');
                CustomerAppointment.close();
            },
            error: function (data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }
        });
    }
};
//点击表格姓名查看患者详情
CustomerAppointment.openBilee = function (ide) {
    $.ajax({
        url: Feng.ctxPath + "/consultantTwo/getdcsByCustomerId",
        type: "post",
        data: {
            "id": ide
        },
        success: function (data) {
            if (data == 1) {
                var index = layer.open({
                    type: 2,
                    title: '到店客户详情',
                    area: ['100%', '100%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/customerDetails/to_openBileLists/' + ide + "/0"
                });
                this.layerIndex = index;
            }else{
                alert("请先联系前台客服匹配建档！");
                return;
            }
        }
    });
};
//关闭窗口
CustomerAppointment.close = function () {parent.layer.close(parent.layer.getFrameIndex(window.name));};
// 检查是否选中
CustomerAppointment.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {Feng.info("请先选中表格中的某一记录！");return false;}
    else {CustomerAppointment.seItem = selected[0];return true;}
};
//刷新 + 统计
CustomerAppointment.search = function () {CustomerAppointment.table.refresh({query: CustomerAppointment.formParams()});CustomerAppointment.total();};
//统计
CustomerAppointment.total = function () {
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var valueOne = multipleSelection($('#oneChannel').val());
    var valueTwo = multipleSelection($('#twoChannel').val());
    var valueThr = multipleSelection($('#thrChannel').val());
    var fieldTc = $("#fieldTc").val();
    var doctor = $("#doctor").val();
    var treatmentUnit = $("#treatmentUnit").val();
    var sreach = $("#sreach").val();
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/customerAppointment/total",
        data: {
            startTime: startTime,
            endTime: endTime,
            oneChannelValue: valueOne,
            twoChannelValue: valueTwo,
            thrChannelValue: valueThr,
            fieldTc: fieldTc,
            doctor: doctor,
            treatmentUnit: treatmentUnit,
            sreach: sreach,
        },
        dataType: "json",
        success: function (data) {
            $("#total").html(data[0].countNum);//总人数
            $("#further").html(data[1].countNum);//复诊预约
            $("#new").html(data[2].countNum);//新诊预约
        }
    });
};
//点击上方条件筛选表格
CustomerAppointment.totalNumber = function (ojb) {
    document.getElementById("customerStatus").value = ojb;
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['oneChannel'] = multipleSelection($('#oneChannel').val());
    queryData['twoChannel'] = multipleSelection($('#twoChannel').val());
    queryData['thrChannel'] = multipleSelection($('#thrChannel').val());
    queryData['sreach'] = $("#sreach").val();
    queryData['customerStatus'] = ojb;
    CustomerAppointment.search();
};
// 明日
CustomerAppointment.tomorrow = function () {
    $("#startFilingTime").val(setDateTime(new Date(),-1) + " " + "00:00:00");
    $("#endFilingTime").val(setDateTime(new Date(),-1) + " " + "23:59:59");
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    CustomerAppointment.search();
};
// 点击上面状态筛选表格
CustomerAppointment.today = function () {
    $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    CustomerAppointment.search();
};
// 明天
CustomerAppointment.tomorrow = function () {
    $("#startTime").val(setDateTime(new Date(),-1) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),-1) + " " + "23:59:59");
    CustomerAppointment.search();
};
// 本周
CustomerAppointment.thisWeek = function () {
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {stepSunDay = -7;}
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    CustomerAppointment.search();
};

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#CustomerAppointmentTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = CustomerAppointment.initColumn();
    var table = new BSTable(CustomerAppointment.id, "/customerAppointment/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CustomerAppointment.formParams());
    CustomerAppointment.table = table.init();
    menuNameHideColumn($("#menuName").val(),'CustomerAppointmentTable');
    $("#oneChannel").change(function () {$("#twoChannel").val('请选择');$("#thrChannel").val('请选择');CustomerAppointment.search();});
    $("#twoChannel").change(function () {$("#thrChannel").val('请选择');CustomerAppointment.search();});
    $("#thrChannel").change(function () {CustomerAppointment.search();});
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {CustomerAppointment.search();});
    checkDate = function () {CustomerAppointment.search();};
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    $("#totalCount").addClass('actionClick').siblings().removeClass('actionClick');
    CustomerAppointment.today();
    phonePrivileges();
});